
'use strict';

const Controller = require('egg').Controller;

class DishController extends Controller {
  async createDish() {
    const params = this.ctx.request.body;
    const result = await this.ctx.service.dish.dish.createDish(
        params.name,
        params.description,
        params.picUrl,
        params.type,
        params.price
    ); 

    if(result){
        this.ctx.code = 200
        this.ctx.body = 'success';
    }else{
        this.ctx.code = 300
        this.ctx.body = 'error';
    }
  }

  async getDishList(){
      const result = await this.ctx.service.dish.dish.dishList();
      if(result != 'error'){
          this.ctx.code = 200;
          this.ctx.body = result;
      }else{
          this.ctx.code = 300;
          this.ctx.body = 'error';
      }
  }

  async changeSellStatus(){
      const params = this.ctx.request.body;

      const result = await this.ctx.service.dish.dish.changeSellStatus(params.did,params.status);

      if (result === 'success'){
          this.ctx.code = 200;
      }else{
          this.ctx.code = 301;
      }
      this.ctx.body = result;
  }

  async deleteDish(did){
      const param = this.ctx.request.body.did;

      const result =await this.ctx.service.dish.dish.deleteDish(param);

      if (result === 'success'){
          this.ctx.code = 200;
      }else{
          this.ctx.code = 301;
      }
      this.ctx.body = result;
  }

  async getOnSaleList(){
      const params = this.ctx.request.body;
      const result = await this.ctx.service.dish.dish.getOnSaleList(params.page,params.size);

      if( result === 'error'){
          this.ctx.code = 301;
      }else{
          this.ctx.code = 200;
      }
      this.ctx.body = result;
  }

  async changePrice(){
      const params = this.ctx.request.body;
      const result = await this.ctx.service.dish.dish.changePrice(params.did,params.price);

      if (result === 'success'){
          this.ctx.code = 200;
      }else{
          this.ctx.code = 301;
      }
      this.ctx.body = result;
  }
}

module.exports = DishController;
